package satk.beans.view;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import satk.beans.session.EventoActivo;
import satk.objetos.ControlCategoria;

/**
 *
 * @author NightNote
 */
@ManagedBean
@ViewScoped
public class AdminControlCategoriaBean
        extends satk.beans.ProcessDataBase<ControlCategoria>
        implements java.io.Serializable {

    private ControlCategoria newCatTorneo = null;
    private Integer identificadorEvento = 0;

    public AdminControlCategoriaBean() {
        super();
        newCatTorneo = new ControlCategoria();
        this.identificadorEvento = EventoActivo.getSessionInstance().getIdentificador();
        newCatTorneo.setIdEvento(identificadorEvento);
    }

    public ControlCategoria getNewCatTorneo() {
        return newCatTorneo;
    }

    @Override
    public String add(ControlCategoria e) {
        resultError = false;
        String $result = null;
        
        if (!e.isSexFemenil() && !e.isSexVaronil()){
            resultMessage = "Seleccione almenos un sexo";
            resultError = true;
            return null;
        }
        
        if(!e.isModKae() && !e.isModKai() && !e.isModKue() && !e.isModKuo()){
            resultMessage = "Seleccione almenos un sexo";
            resultError = true;
            return null;
        }
        
        try {
            resultCode = new satk.database.ManagerControlCategoria().add(e);
            if (resultCode == -5) {
                resultMessage = "Ya existe una categoira con el mismo nombre y cinta";
                resultError = true;
            } else {
                resultMessage = String.format("[%s %s] Registrada exitosamente",
                        e.getCategoria().getNombre(), e.getCinta().getColor());
                super.getLista().add(0, e);
                e = new ControlCategoria();
                e.setIdEvento(identificadorEvento);
                $result = "nvaCategoriaTorneo";
            }
        } catch (java.sql.SQLIntegrityConstraintViolationException ex) {
            Logger.getLogger(AdminControlCategoriaBean.class.getName()).log(Level.SEVERE, "Violacion de integridad", ex);
            resultCode = ex.getErrorCode();
            resultMessage = ex.getMessage();
            resultError = true;
        } catch (java.sql.SQLException ex) {
            Logger.getLogger(AdminControlCategoriaBean.class.getName()).log(Level.SEVERE, "Error en la Inscripción", ex);
            resultCode = ex.getErrorCode();
            resultMessage = ex.getSQLState() + " " + ex.getMessage();
            resultError = true;
        }
        result = true;
        return $result;
    }

    @Override
    public String edit(ControlCategoria o, ControlCategoria n) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public String del(ControlCategoria e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public String actionSaveEdit(ControlCategoria e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public String actionSaveEditAll() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public String actionSaveRemove(ControlCategoria e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public String actionSaveRemoveAll() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
